package com.gxa.zhonglong.config;


import com.alibaba.fastjson.TypeReference;
import com.fasterxml.classmate.TypeResolver;
import org.ehcache.shadow.org.terracotta.offheapstore.paging.Page;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.AlternateTypeRuleConvention;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.awt.print.Pageable;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static springfox.documentation.schema.AlternateTypeRules.newRule;

/**
<<<<<<< HEAD
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/25 13:57
 * Description:
 * Version: V1.0
=======
 * @author wanjun
 * @version 1.0
 * @date 2021/10/22 10:23
>>>>>>> origin/master
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.zhonglong"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Timestamp.class);    // 忽略Timestamp类型
//                .build().securitySchemes(security());     //设置校验规则
    }

    // 指定每次访问携带token
    /*private List<ApiKey> security(){
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("token","token","header"));
        return apiKeyList;
    }*/

    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("中隆黄金进销存系统API文档")
                .description("牛马文档")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

    @Bean
    public AlternateTypeRuleConvention pageableConvention(final TypeResolver resolver) {
        return new AlternateTypeRuleConvention() {

            @Override
            public int getOrder() {
                return Ordered.HIGHEST_PRECEDENCE;
            }

            @Override
            public List<AlternateTypeRule> rules() {
                Type originalType = new TypeReference<List<Timestamp>>() {}.getType();
                Type alternateType = new TypeReference<List<Date>>() {}.getType();
                /*return newArrayList(
                        newRule(resolver.resolve(Pageable.class), resolver.resolve(Page.class)),
                        newRule(resolver.resolve(Timestamp.class), resolver.resolve(Date.class)),
                        newRule(originalType, alternateType)
                );*/

                List<AlternateTypeRule> list = new ArrayList();
                list.add(newRule(resolver.resolve(Pageable.class), resolver.resolve(Page.class)));
                list.add(newRule(resolver.resolve(Timestamp.class), resolver.resolve(Date.class)));
                list.add(newRule(originalType, alternateType));
                return list;
            }
        };
    }
}
